<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px" class="searchtop">
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="queryParams.userName" placeholder="请输入用户账号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="queryParams.email" placeholder="请输入用户邮箱" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="queryParams.mobile" placeholder="请输入手机号码" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="头像地址" prop="avatar">
          <el-input v-model="queryParams.avatar" placeholder="请输入头像地址" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <!-- <el-form-item label="密码" prop="password">
          <el-input v-model="queryParams.password" placeholder="请输入密码" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <!-- <el-form-item label="盐" prop="salt">
          <el-input v-model="queryParams.salt" placeholder="请输入盐" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <!-- <el-form-item label="最后登录IP" prop="loginIp">
          <el-input v-model="queryParams.loginIp" placeholder="请输入最后登录IP" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <!-- <el-form-item label="最后登录时间" prop="loginDate">
          <el-date-picker clearable v-model="queryParams.loginDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="openid" prop="openid">
          <el-input v-model="queryParams.openid" placeholder="请输入openid" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt20">

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">用户管理</span></el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            v-hasPermi="['wms:mailboxappuser:add']">新增</el-button>
          <el-button type="warning" plain icon="Download" @click="handleExport"
            v-hasPermi="['wms:mailboxappuser:export']">导出</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="mailboxappuserList" border class="mt20">
        <el-table-column type="index" width="60" align="center" label="序号" fixed="left" />
        <!-- <el-table-column label="用户账号" prop="userName" align="center" /> -->
        <el-table-column label="用户昵称" prop="nickName" align="center" />
        <el-table-column label="用户邮箱" prop="email" align="center" >
          <template #default="scope">
            <div>{{scope.row.email||'--'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="手机号码" prop="mobile" align="center" >
          <template #default="scope">
            <div>{{scope.row.mobile||'--'}}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="性别" prop="sex" align="center">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
          </template>
        </el-table-column> -->

        <!-- <el-table-column label="密码" prop="password" /> -->
        <!-- <el-table-column label="盐" prop="salt" /> -->
        <el-table-column label="账号状态" align="center" width="100"  key="status">
          <template #default="scope">
            <el-switch v-model="scope.row.status" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="地区" prop="area" align="center" width="220" >
          <template #default="scope">
            <div>{{scope.row.area||'--'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="最后登录IP" prop="loginIp" align="center"  />
        <el-table-column label="最后登录时间" prop="loginDate" width="180" align="center">
          <template #default="scope">
            <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="备注" prop="remark" /> -->
        <!-- <el-table-column label="openid" prop="openid" width="250" align="center" /> -->
        <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)"
              v-hasPermi="['wms:mailboxappuser:edit']">查看</el-button>
            <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['wms:mailboxappuser:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>

    </el-card>
    <!-- 添加或修改用户管理对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="mailboxappuserRef" :model="form" :rules="rules" label-width="120px" class="searchtop1">
        <el-form-item label="头像地址" style="width: 100%;">
          <img :src="form.avatar" alt="" />
          <!-- <el-input style="width: 100%;" disabled v-model="form.avatar" placeholder="请输入头像地址" /> -->
        </el-form-item>
        <!-- <el-form-item label="用户账号">
          <el-input style="width: 100%;" disabled v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item> -->
        <el-form-item label="用户性别">
          <el-select style="width: 100%;" disabled v-model="form.sex" placeholder="请选择性别">
            <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input style="width: 100%;" disabled v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户邮箱">
          <el-input style="width: 100%;" disabled v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input style="width: 100%;" disabled v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>

        <!-- <el-form-item label="密码">
          <el-input style="width: 100%;" disabled v-model="form.password" placeholder="请输入密码" />
        </el-form-item> -->

        <el-form-item label="最后登录IP">
          <el-input style="width: 100%;" disabled v-model="form.loginIp" placeholder="请输入最后登录IP" />
        </el-form-item>
        <el-form-item label="最后登录时间">
          <el-date-picker style="width: 100%;" disabled clearable v-model="form.loginDate" type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="openid">
          <el-input v-model="form.openid" disabled placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" style="width: 100%;">
          <el-input style="width: 100%;" disabled v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Mailboxappuser">
  import {
    listMailboxappuser,
    getMailboxappuser,
    delMailboxappuser,
    addMailboxappuser,
    updateMailboxappuser,
    changeUserStatus
  } from "@/api/wms/mailboxappuser";


  const {
    proxy
  } = getCurrentInstance();
  const {

    sys_user_sex

  } = proxy.useDict('sys_user_sex');
  const mailboxappuserList = ref([]);
  const open = ref(false);
  const buttonLoading = ref(false);
  const loading = ref(true);
  const ids = ref([]);
  const total = ref(0);
  const title = ref("");

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      userName: undefined,
      nickName: undefined,
      email: undefined,
      mobile: undefined,
      sex: undefined,
      avatar: undefined,
      password: undefined,
      salt: undefined,
      status: undefined,
      loginIp: undefined,
      loginDate: undefined,
      openid: undefined
    },
    rules: {
      userId: [{
        required: true,
        message: "用户ID不能为空",
        trigger: "blur"
      }],
      userName: [{
        required: true,
        message: "用户账号不能为空",
        trigger: "blur"
      }],
      nickName: [{
        required: true,
        message: "用户昵称不能为空",
        trigger: "blur"
      }],
      email: [{
        required: true,
        message: "用户邮箱不能为空",
        trigger: "blur"
      }],
      mobile: [{
        required: true,
        message: "手机号码不能为空",
        trigger: "blur"
      }],
      sex: [{
        required: true,
        message: "用户性别不能为空",
        trigger: "change"
      }],
      avatar: [{
        required: true,
        message: "头像地址不能为空",
        trigger: "blur"
      }],
      password: [{
        required: true,
        message: "密码不能为空",
        trigger: "blur"
      }],
      salt: [{
        required: true,
        message: "盐不能为空",
        trigger: "blur"
      }],
      status: [{
        required: true,
        message: "帐号状态不能为空",
        trigger: "change"
      }],
      loginIp: [{
        required: true,
        message: "最后登录IP不能为空",
        trigger: "blur"
      }],
      loginDate: [{
        required: true,
        message: "最后登录时间不能为空",
        trigger: "blur"
      }],
      remark: [{
        required: true,
        message: "备注不能为空",
        trigger: "blur"
      }],
      openid: [{
        required: true,
        message: "openid不能为空",
        trigger: "blur"
      }]
    }
  });

  const {
    queryParams,
    form,
    rules
  } = toRefs(data);

  /** 查询用户管理列表 */
  function getList() {
    loading.value = true;
    listMailboxappuser(queryParams.value).then(response => {
      mailboxappuserList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      userId: null,
      userName: null,
      nickName: null,
      email: null,
      mobile: null,
      sex: null,
      avatar: null,
      password: null,
      salt: null,
      status: null,
      delFlag: null,
      loginIp: null,
      loginDate: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      remark: null,
      openid: null
    };
    proxy.resetForm("mailboxappuserRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加用户管理";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _userId = row.userId || ids.value
    getMailboxappuser(_userId).then(response => {
      form.value = response.data;
      console.log(response, "response")
      form.value.sex = Number(response.data.sex);
      open.value = true;
      title.value = "修改用户管理";
    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["mailboxappuserRef"].validate(valid => {
      if (valid) {
        buttonLoading.value = true;
        if (form.value.userId != null) {
          updateMailboxappuser(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          }).finally(() => {
            buttonLoading.value = false;
          });
        } else {
          addMailboxappuser(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          }).finally(() => {
            buttonLoading.value = false;
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _userIds = row.userId || ids.value;
    proxy.$modal.confirm('是否确认删除用户管理编号为"' + _userIds + '"的数据项？').then(function() {
      loading.value = true;
      return delMailboxappuser(_userIds);
    }).then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {}).finally(() => {
      loading.value = false;
    });
  }

  function handleStatusChange(row) {
    let text = row.status === "1" ? "启用" : "停用";
    proxy.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗?').then(function() {
      return changeUserStatus(row.userId, row.status);
    }).then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    }).catch(function() {
      row.status = row.status === "0" ? "1" : "0";
    });
  }
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('wms/mailboxappuser/export', {
      ...queryParams.value
    }, `mailboxappuser_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
<style scoped>
  .searchtop {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }

  .searchtop .el-form-item {
    width: 18%;
  }

  .searchtop1 {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }

  .searchtop1 .el-form-item {
    width: 50%;
  }
</style>
